package com.demo.crm.springboot3security.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.crm.springboot3security.entity.FormFieldValue;
import com.demo.crm.springboot3security.mapper.FormFieldValueMapper;
import com.demo.crm.springboot3security.service.FormFieldValueService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

@Service
public class FormFieldValueServiceImpl implements FormFieldValueService {

    @Autowired
    private FormFieldValueMapper formFieldValueMapper;

    @Override
    public List<FormFieldValue> findByFormIdAndRecordId(Long formId, Long recordId) {
        return formFieldValueMapper.findByFormIdAndRecordId(formId, recordId);
    }

    @Override
    public List<FormFieldValue> findByFormIdAndFieldIdAndRecordIdIn(Long formId, Long fieldId, List<Long> recordIds) {
        return formFieldValueMapper.findByFormIdAndFieldIdAndRecordIdIn(formId, fieldId, recordIds);
    }

    @Override
    public List<Long> findRecordIdsByTextSearch(Long formId, Long fieldId, String searchText) {
        return formFieldValueMapper.findRecordIdsByTextSearch(formId, fieldId, searchText);
    }

    @Override
    @Transactional
    public FormFieldValue save(FormFieldValue value) {
        if (value.getId() == null) {
            value.setCreatedAt(LocalDateTime.now());
            value.setUpdatedAt(LocalDateTime.now());
            formFieldValueMapper.insert(value);
        } else {
            value.setUpdatedAt(LocalDateTime.now());
            formFieldValueMapper.update(value);
        }
        return value;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        formFieldValueMapper.deleteById(id);
    }

    @Override
    public FormFieldValue findById(Long id) {
        return formFieldValueMapper.selectOneById(id);
    }

    @Override
    public List<FormFieldValue> findAll() {
        return formFieldValueMapper.selectAll();
    }

    @Override
    public Page<FormFieldValue> findByPage(int pageNumber, int pageSize) {
        return formFieldValueMapper.paginate(pageNumber, pageSize, QueryWrapper.create());
    }

    @Override
    @Transactional
    public void deleteByFormIdAndRecordId(Long formId, Long recordId) {
        formFieldValueMapper.deleteByFormIdAndRecordId(formId, recordId);
    }

    @Override
    @Transactional
    public List<FormFieldValue> saveAll(List<FormFieldValue> values) {
        LocalDateTime now = LocalDateTime.now();
        for (FormFieldValue value : values) {
            if (value.getId() == null) {
                value.setCreatedAt(now);
                value.setUpdatedAt(now);
                formFieldValueMapper.insert(value);
            } else {
                value.setUpdatedAt(now);
                formFieldValueMapper.update(value);
            }
        }
        return values;
    }
}